package com.guyaogg.gulimall.cart.contoller;

import com.guyaogg.gulimall.cart.service.CartService;
import com.guyaogg.gulimall.cart.vo.Cart;
import com.guyaogg.gulimall.cart.vo.CartItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author 顾遥
 */
@Slf4j
@Controller
public class CartController {


    private final CartService cartService;

    public CartController(CartService cartService) {
        this.cartService = cartService;
    }

    @ResponseBody
    @GetMapping("/currentUserCartItems")
    public List<CartItem> getCurrentUserCartItems(){
        return cartService.getUserCartItems();
    }

    @GetMapping("deleteItem.html")
    public String deleteItem(@RequestParam("skuId")Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";

    }
    @GetMapping("countItem.html")
    public String countItem(@RequestParam("skuId")Long skuId,
                            @RequestParam("count")Integer count){
        cartService.changeCountItem(skuId,count);
        return "redirect:http://cart.gulimall.com/cart.html";

    }
    @GetMapping("checkItem.html")
    public String checkItem(@RequestParam("skuId")Long skuId,
                            @RequestParam("check")Integer check){
        cartService.checkItem(skuId,check);
        return "redirect:http://cart.gulimall.com/cart.html";

    }
    /**
     * 去购物车页面
     * cookie 存有 user-key 保存临时购物车，有效期 1 个月
     * @return
     */
    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
        // 快速获取用户信息
//        UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
//        log.debug("{}", userInfoTo);
       Cart cart =  cartService.getCart();
       model.addAttribute("cart", cart);
        return "cartList";
    }

    /**
     * 添加商品到购物车
     * @return
     */
    @GetMapping("addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("count")Integer count,
                            RedirectAttributes ra) throws ExecutionException, InterruptedException {
        log.debug("sku:{},count:{}", skuId,count);

        cartService.addToCart(skuId,count);
        ra.addAttribute("skuId",skuId);

        // 重定向防止多刷
        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }

    /**
     * 查询添加数据
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId,Model model){

        // 重定向到成功页面。再次查询购物车数据
       CartItem cartItem =  cartService.getCartItem(skuId);
       model.addAttribute("item", cartItem);
        return "success";
    }
}
